package com.urerp.web.interceptor;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.urerp.web.common.SessionUtil;
import com.urerp.web.persistence.MenuMapper;

@Service
public class SessionInterceptor extends HandlerInterceptorAdapter {
	
	@Autowired
	private MenuMapper menuMapper;
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
		
		if(!SessionUtil.getSession()) {
			
			try {
				response.sendRedirect(request.getContextPath() + "/login/loginForm.ur");
				return false;
				
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		
        String menu_id = request.getParameter("code");
    	
    	if (menu_id != null) {
    		request.setAttribute("pageHeader", menuMapper.getPageHeader(menu_id));
    	}
		
		return true;
	}
	
}
